package com.mindskip.xzs.controller.admin;

import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.domain.dto.admin.question.QuestionDTO;
import com.mindskip.xzs.domain.dto.admin.question.QuestionPageDTO;
import com.mindskip.xzs.domain.vo.admin.question.QuestionPageVO;
import com.mindskip.xzs.domain.vo.admin.question.QuestionSelectVO;
import com.mindskip.xzs.service.QuestionService;
import com.mindskip.xzs.utility.RestResponse;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController("AdminQuestionController")
@RequestMapping(value = "/api/admin/question")
public class QuestionController {

    private final QuestionService questionService;

    public QuestionController(QuestionService questionService) {
        this.questionService = questionService;
    }

    @PostMapping("/page")
    public RestResponse<PageInfo<QuestionPageVO>> page(@RequestBody QuestionPageDTO questionPageDTO) {

        PageInfo<QuestionPageVO> pageInfo = questionService.getQuestionByPage(questionPageDTO);

        return RestResponse.ok(pageInfo);
    }

    @PostMapping("/select/{id}")
    public RestResponse<QuestionSelectVO> select(@PathVariable Integer id) {

        QuestionSelectVO questionSelectVO = questionService.questionToQuestionSelectVO(id);

        return RestResponse.ok(questionSelectVO);
    }

    @PostMapping("/edit")
    public RestResponse<Object> edit(@RequestBody @Valid QuestionDTO questionDTO) {
        questionService.insertOrUpdateQuestion(questionDTO);
        return RestResponse.ok();
    }

    @PostMapping("/deleted/{id}")
    public RestResponse<Object> deleted(@PathVariable Integer id) {

        questionService.softDeleted(id);

        return RestResponse.ok();
    }
}
